import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-04-29
 * Time: 9:29
 */

//数组的定义初始化
public class TestDome1 {
    public static void main(String[] args) {
        int[][] array = new int[2][];//数组里什么都没有时默认为null

        //单独为每行赋值
        //不规则二维数组
        array[0] = new int[]{1,2,3};
        array[1] = new int[]{11,222,4,555,666787,88};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

    }
    public static void main11(String[] args) {
        //二维数组定义
        int[][] array = {{1,2,3},{4,5,6},{7,8,9}};
        int[][] array1 = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        int[][] array2 = new int[2][3];// 行不可省,列可省

        //二维数组是特殊的一维数组,此时的 array[0] 是第一行一维数组的地址,array[1] 是第二行一维数组的地址
        //二维数组的个数 array.length 代表的是行的个数
        //二维数组的每一行存储的是当前行的地址,每一行代表一个一维数组的引用,
        System.out.println("存储在二维数组中每行的内容是:");
        System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);

        System.out.println("使用 Arrays.deepToString 打印二维数组:");
        System.out.println(Arrays.deepToString(array));

        System.out.println("使用 Arrays. toString 打印二维数组:");
        //打印二维数组的每一行的元素
        System.out.println(Arrays.toString(array[0]));
        System.out.println(Arrays.toString(array[1]));
        System.out.println(Arrays.toString(array[2]));

        System.out.println("for 循环遍历:");
        //for循环遍历二维数组
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("for-each 循环遍历:");
        //for-each循环遍历二维数组
        //数组的每一个元素,当前二维数组的每个元素时一个一维数组,所以这里用一维数组接收 : 数组名
        for (int[] ret : array) {
            //  一维数组的每个元素 : 一维数组
            for (int x : ret) {
                System.out.print(x +" ");
            }
            System.out.println();
        }
    }
    public static void main10(String[] args) {
        //比较字符串
        int[] array1 = {1,2,3,4};
        int[] array2 = {1,2,3,4};
        System.out.println(Arrays.equals(array1,array2));

        //字符串赋初始值 也可范围赋值
        int[] array3 = new int[10];
//        Arrays.fill(array3,-5);
        Arrays.fill(array3,1,4,-5);//[1,4)
        System.out.println(Arrays.toString(array3));

    }
    public static void main9(String[] args) {
        //拷贝字符串
        //也可以当作扩容来用
        int[] array = {1,2,3,4,5};
//        int[] ret  = Arrays.copyOf(array,array.length*2);
        int[] ret = new int[array.length];
        //System。arraycopy 这个方法是 Arrays.copyOf 底层的方法 由C/C++写的
        //Object src 原始数组,  int srcPos 原始数组的起始位置, Object dest 目标数组, int destPos 目标数组的起始位置, int length 拷贝长度
        System.arraycopy(array,0,ret,0,array.length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }
    public static String myToString(int[] array) {

        if(array == null) {
            return "null";
        }

        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret = ret + array[i];
            if(i != array.length-1) {
                ret = ret + ",";
            }
        }
        ret = ret + "]";
        return ret;
    }
    public static void main8(String[] args) {
        int[] array = null;
        //字符串打印
        System.out.println(myToString(array));
    }

    public static int[] grow(int[] array) {
        int[] tmpArr = new int[array.length];
        for (int i = 0; i < tmpArr.length; i++) {
            tmpArr[i] = array[i]*2;
        }
        return tmpArr;
    }
    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5};
        //数组传参
        int[] retArr = grow(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(retArr));

    }

    public static int[] func3() {
        //数组作为返回值
        return new int[]{11,22,33,44};
    }
    public static void main6(String[] args) {
        int[] array = func3();
        System.out.println(Arrays.toString(array));
    }

    public static void func1(int[] array) {
        //只是改变了形参的指向,并没有改变实参的指向
        array = new int[]{11,22,33,44};
    }
    public static void func2(int[] array) {
        //传递的是引用,通过这个引用改变了原来的值
        array[0] = 999;
    }
    public static void main5(String[] args) {
          int[] array1 = {1,2,3,4};
          func1(array1);
          func2(array1);
        System.out.println(Arrays.toString(array1));

    }

    public static void main4(String[] args) {
        int[] array1 = {1,2,3,4};
        array1[0] = 99;

        //将array1指向的对象给array2,此时array2也指向了array1
        int[] array2 = array1;
        array2[0] = 100;

        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }
    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5,6};
        //输出数组元素
        //使用for循环
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }

        System.out.println("===========");

        //使用for-each
        for (int x : array) {
            System.out.println(x);
        }

        System.out.println("===========");

        //使用类Arrays中的方法toString,以字符串形式打印数组元素
        String ret = Arrays.toString(array);
        System.out.println(ret);

    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5,6};
        //输出数组元素个数
        System.out.println(array.length);

    }
    public static void main1(String[] args) {
        //数组属于引用类型,所以在定义时候可以new,但是为了简便可以直接定义
        //三种定义数组的方式
        int[] array1 = {1,2,3,4,5,6,7,8,9,10};
        int[] array2 = new int[]{1,2,3,4,5,6,7,8,9,10};
        int[] array3 = new int[10];
    }
}
